$(function(){
    new FastClick(document.body);
    //节流阀
    var flag =false;
    //获取分类ID
    var categoryId = tools.queryString().clsId;
    var midPicUrl = tools.queryString().picUrl;
    var title = decodeURIComponent(tools.queryString().title);
    var pageNum = 1;
    var sort = 1;
    $(".classifyPic img").attr('src',midPicUrl);
    $(".header span").text(title);
    //按钮样式切换
    $(".sBtn").on("click",function(){
        pageNum = 1;
        $(".sBtn").removeClass("actived");
        $(this).addClass("actived");
        sort = $(this).attr("index");
        loadClassifyProduct(categoryId,sort);
    });
    //历史回退
    $(".backArrow").on("click",function(){
        history.back();
    });
    function loadClassifyProduct(categoryId,sort){
        if (flag) return false;
        $.ajax({
            url: domain.test + "/getcategoryproduct",
            // url: domain.online + "/api/product/list",
            type: 'get',
            data: "categoryId=" + categoryId + '&pageNum=' + pageNum + "&sort=" + sort,
            dataType: 'json',
            beforeSend: function () {
                flag = true;
            },
            success: function (response) {
                if(response.data.rtnCode===200) {
                // if(response.rtnCode===200) {
                    var classifyProduct = response.data.data;
                    // var classifyProduct = response.data;
                    if (pageNum === 1) {
                        $('.productItemBox').html(template('productItemBox', { productList: classifyProduct.plist }));
                    } else {
                        $('.productItemBox').append(template('productItemBox', { productList: classifyProduct.plist }));
                    }
                    pullmaster.pullDownSuccess();       //刷新成功
                    if (classifyProduct.plist.length < 10 || classifyProduct.plist.length === 0) {
                        pullmaster.pullUpDone();
                        $(".bb_pull-up").css({"display":"none"});
                        $(".end").css({"display":"block"});
                    }
                    ++pageNum;
                    flag = false;
                }
            },
            error: function () {
                pullmaster.pullDownFailed();        //刷新失败
                pullmaster.pullUpFailed();          //数据加载失败
            }
        })
    }
    var pullmaster = new Pull($('.productList'), {
        onPullDown: function () {
            pageNum = 1;
            loadClassifyProduct(categoryId,sort);
        },
        onPullUp: function () {
            loadClassifyProduct(categoryId,sort);
        }
    });
    //查看详情
    $(".productList").on("click",".productItem",function(){
        var pId = $(this).attr("data-id");
        location.href = "./product.html?id=" + pId;
    });
})